//
//  UIViewController+ProgressView.m
//  nav
//
//  Created by wjxfhxy on 15/8/11.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "UIViewController+ProgressView.h"


@interface UIViewController (ProgressViewInternal)<CProgressViewPanelDelegate>

@property(nonatomic,readonly) CProgressViewPanelHelper *progressViewHelper;

@end

@implementation UIViewController (ProgressView)

- (CProgressViewPanelHelper*)progressViewHelper
{
    CProgressViewPanelHelper *helper = [self associatedValueForKey:"progressViewHelper"];
    if(helper == nil)
    {
        helper = [[CProgressViewPanelHelper new] autorelease_mm];
        [self setAssociatedRetainNonatomicValue:helper withKey:"progressViewHelper"];
    }
    return helper;
}

- (void)beginProgressViewPanel:(NSString *)title
{
    [self beginProgressViewPanel:title completion:NULL];
}

- (void)beginProgressViewPanel:(NSString *)title completion:(void (^)())completion
{
    DispatchMainThread(NO, ^
    {
        for(UIBarButtonItem *item in self.navigationItem.rightBarButtonItems)
        {
            item.enabled = NO;
        }
        
        [self.progressViewHelper progressViewPanelHelperWithActivity:YES title:title text:nil delegate:self];
        if(completion != NULL)
            completion();
    });
}

- (void)retainProgressViewPanel
{
    [self.progressViewHelper retainProgressViewPanel];
}

- (void)endProgressViewPanel
{
    [self endProgressViewPanelCompletion:NULL];
}

- (void)endProgressViewPanelCompletion:(void (^)())completion
{
    DispatchMainThread(NO, ^
    {
        for(UIBarButtonItem *item in self.navigationItem.rightBarButtonItems)
        {
            item.enabled = YES;
        }
        
        [self.progressViewHelper releaseProgressViewPanel];
        if(completion != NULL)
            completion();
    });
}

- (void)beginProgressViewPanel
{
    [self beginProgressViewPanel:@"数据更新中"];
}

- (void)setProgressViewPanelReleaseDelayTime:(NSTimeInterval)delayTime
{
    self.progressViewHelper.delayReleaseTime = delayTime;
}

- (void)setProgressViewPanelText:(NSString*)text
{
    [self.progressViewHelper setText:text];
}

- (void)progressViewPanelDidRemove:(CProgressViewPanel *)pvp
{
}

- (UIView *)getProgressViewPanelParent:(CProgressViewPanel *)pvp
{
    return self.view;
}

- (CGRect)getProgressViewPanelCoverRect:(CProgressViewPanel *)pvp
{
    return self.view.bounds;
}

- (CGPoint)getProgressViewCenterPosition:(CProgressViewPanel *)pvp
{
    return self.view.center;
}

- (void)touchProgressViewPanel:(CProgressViewPanel *)pvp
{
}

@end
